import { shopList, total, gsdlist, showArea } from "../../server/api";
import {openMap,chooseMap} from '../../server/pulbmethod'
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    screenHeight: app.globalData.screenHeight,
    statusBarHeight: app.globalData.CustomBar,
    jnheight:wx.getMenuButtonBoundingClientRect().height,
    sjheight: "",
    commodityName: "", //商品名称
    placeOfBelonging: "", //归属地编码
    pageNo: "1",
    pageSize: "10",
    list: [],
    kyjf: "", //可用积分
    show: false,
    columns: [],
    defaultIndex: 0,
    cname: "",
    topHeight:wx.getMenuButtonBoundingClientRect().top,//顶部到胶囊的高度
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options, "options");
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getTabBar().toggleMenu(0)
    this.setData({
      commodityName: "", //商品名称
      pageNo: "1",
      pageSize: "10",
      list: [],
    });

    this.getTabBar().init();
    let windowHeight = wx.getSystemInfoSync().windowHeight;
    let windowWidth = wx.getSystemInfoSync().windowWidth;
    this.setData({
      sjheight: (windowHeight * 750) / windowWidth - (330 + 560),
    });
    this.getdata();
  },
  onChange(event) {
    const { picker, value, index } = event.detail;
    if (index == 0) {
      picker.setColumnValues(1, value[0].children == null ? [] : value[0].children);
      picker.setColumnValues(2,value[0].children == null ? [] : value[0].children[0].children == null ? [] : value[0].children[0].children);
    } else if (index === 1) {
      picker.setColumnValues(2, value[1].children == null ? [] : value[1].children);
    }
  },
  onCancel() {
    this.setData({ show: false });
  },
  async onConfirm(event) {
    const res = event.detail.value;
    const useraddress =
    (res[2] == undefined && res[1] == undefined) ?
     res[0].pointsPlaceName  :
      res[0].pointsPlaceName +
      ( 
      res[1] == undefined ? "-" + res[0].pointsPlaceName : "-" + res[1].pointsPlaceName )
     +
      (res[2] == undefined
        ? ""
        : "-" + res[2].pointsPlaceName);

    this.setData({
      list: [],
      cname: useraddress,
      show: false,
      placeOfBelonging:(res[2] == undefined && res[1] == undefined) ? res[0].id : res[2] == undefined ? res[1].id : res[2].id,
      pageNo: 1,
    });
    this.getdata();
  },
  //获取商品列表数据
  async getdata(objdata) {
    const params = {
      pageNo: objdata == undefined ? this.data.pageNo : objdata.pageNo,
      pageSize: objdata == undefined ? this.data.pageSize : objdata.pageSize,
      commodityName: this.data.commodityName,
    };
    const res = await shopList(params);
    if (res.code == 200) {
      const data = res.result.records;
      this.setData({ list: [...this.data.list, ...data] });
    }
    const list = await total();
    if (list.code == 200) {
      this.setData({ kyjf:list.result == null ? '' : list.result.balance });
    }
  },
  //滚动条事件
  getMore(e) {
    this.data.pageNo++;
    let params = {
      pageNo: this.data.pageNo,
      pageSize: this.data.pageSize,
    };
    this.getdata(params);
  },
  //输入时触发
  onInput(e) {
    this.setData({ commodityName: e.detail.value });
  },
  //搜索事件
  onSearch(e) {
    this.setData({ list: [], pageNo: "1", pageSize: "10" });
    this.getdata();
  },
  Pending() {
    wx.navigateTo({
      url: "/subPackagesA/pages/shoporder/shoporder",
    });
  },
  jfdetail() {
    wx.setStorageSync('count', 5)
    wx.setStorageSync('active', '2')
    wx.switchTab({
      url: "/pages/pointcenter/pointcenter",
    });
  },
  productdetail(e) {
    const spid = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/subPackagesA/pages/shoplist/shoplist?id=${spid}`,
    });
  },
  //打开地图
  openadd(e){
    const obj = e.currentTarget.dataset.meg
    console.log(obj,'obj');
    openMap(obj)
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
